package com.example.chuqq.gpssigninapplication;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import java.util.Timer;
import java.util.TimerTask;

public class WatcherService extends Service {
    private static final String TAG = "WatcherService";

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand()");

        new Thread(new Runnable() {
            @Override
            public void run() {
                Timer timer = new Timer();
                TimerTask task = new TimerTask() {
                    @Override
                    public void run() {
                        Log.d(TAG, "TimerTask");
                        if (!CommonUtil.isServiceRunning(WatcherService.this.getApplicationContext(), MainService.class.getName())) {
                            Intent service = new Intent(WatcherService.this.getApplicationContext(), MainService.class);
                            startService(service);
                        }
                    }
                };
                timer.schedule(task, 0, 1000);
            }
        }).start();
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        // do something...
    }
}

